<?php

namespace App\Controller;

class MallImxsController extends AppController
{

    public $_page_num = 30;

    public function initialize()
    {
        parent::initialize();
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.11.17
   * ps：新增推荐商品
   * */
    public function add()
    {
        $data = $this->request->getData();
        if (empty($data)) {
            $this->G->error("data_error");
            return null;
        }
        $this->loadModel('Products');
        $mall_imx = $this->MallImxs->newEntity();
        $mall_imx->mall_adblock_id = $data['mall_adblock_id'];
        $mall_imx->product_id = $data['product_id'];
        $mall_imx->img = empty($data['img']) ? '' : $this->G->setImgUrl($data['img']);
        $mall_imx->start_date = empty($data['start_date']) ? 0 : strtotime($data['start_date']);
        $mall_imx->end_date = empty($data['end_date']) ? 0 : strtotime($data['end_date']);
        $mall_imx->intro = empty($data['intro']) ? '' : $data['intro'];
        $mall_imx->title = empty($data['title']) ? '' : $data['title'];
        $mall_imx->is_resident = empty($data['is_resident']) ? 0 : $data['is_resident'];
        $mall_imx->order_num = empty($data['order_num']) ? 0 : $data['order_num'];
        $mall_imx->enabled = $data['enabled'] == 1 ? 1 : 0;
        $this->MallImxs->save($mall_imx);
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.11.17
   * ps：修改推荐商品
   * */
    public function update()
    {
        $data = $this->request->getData();
        if (empty($data)) {
            $this->G->error("data_error");
            return null;
        }
        $mall_imxs = $this->MallImxs->findById($data['id'])->select(['id'])->first();
        if (empty($mall_imxs)) {
            $this->G->error("mall_imx_error");
            return null;
        }
        $mall_imx = $this->MallImxs->newEntity();
        $mall_imx->id = $data['id'];
        $mall_imx->mall_adblock_id = $data['mall_adblock_id'];
        $mall_imx->title = $data['title'];
        $mall_imx->product_id = $data['product_id'];
        $mall_imx->img = empty($data['img']) ? '' : $this->G->setImgUrl($data['img']);
        $mall_imx->start_date = empty($data['start_date']) ? 0 : strtotime($data['start_date']);
        $mall_imx->end_date = empty($data['end_date']) ? 0 : strtotime($data['end_date']);
        $mall_imx->intro = empty($data['intro']) ? '' : $data['intro'];
        $mall_imx->is_resident = empty($data['is_resident']) ? 0 : $data['is_resident'];
        $mall_imx->order_num = empty($data['order_num']) ? 0 : $data['order_num'];
        $mall_imx->enabled = $data['enabled'] == 1 ? 1 : 0;
        $this->MallImxs->save($mall_imx);
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.11.17
   * ps：删除首页推荐商品
   * */
    public function delete()
    {
        $id = $this->request->getData('id');
        if (!is_numeric($id)) {
            $this->G->error("id_error");
            return null;
        }
        $this->MallImxs->deleteAll(['id' => $id]);
        $this->G->success("Success");
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.11.17
   * ps：获取推荐商品列表
   * */
    public function getList()
    {
        $page = empty($this->request->getData("page")) ? 1 : $this->request->getData("page");
        $title = $this->request->getData("title");
        $where = [];
        if (!empty($title)) {
            $where['title like'] = '%' . $title . '%';
        }
        $mall_imxs = $this->MallImxs->find()
                                    ->order(['order_num desc'])
                                    ->where(['title like' => '%' . $title . '%'])
                                    ->limit(30)
                                    ->page($page);
        $re = [];
        $count = $mall_imxs->count();
        $re['pageTotal'] = ceil($count / 30);
        $mall_imxs = $mall_imxs->toArray();
        $re['list'] = [];
        if (!empty($mall_imxs)) {
            $this->loadModel('MallAdblocks');
            $mall_adblocks = $this->MallAdblocks->find()
                                                ->select(['id', 'title'])
                                                ->where(['id in' => array_column($mall_imxs, 'mall_adblock_id')])
                                                ->toArray();
            $mall_adblock = [];
            if (!empty($mall_adblocks)) {
                foreach ($mall_adblocks as $k => $v) {
                    $mall_adblock[$v['id']] = $v['title'];
                }
            }
            $this->loadModel('Products');
            $products = $this->Products->find()
                                       ->select(['id', 'title'])
                                       ->where(['id in' => array_column($mall_imxs, 'product_id')])
                                       ->toArray();
            $product = [];
            if (!empty($products)) {
                foreach ($products as $k => $v) {
                    $product[$v['id']] = $v['title'];
                }
            }
            foreach ($mall_imxs as $k => $v) {
                $mall_imxs[$k]['mall_adblock_title'] = empty($mall_adblock[$v['mall_adblock_id']]) ? '' : $mall_adblock[$v['mall_adblock_id']];
                $mall_imxs[$k]['product_title'] = empty($product[$v['product_id']]) ? '' : $product[$v['product_id']];
                $mall_imxs[$k]['start_date'] = empty($v['start_date']) ? '' : date('Y-m-d H:i:s', $v['start_date']);
                $mall_imxs[$k]['end_date'] = empty($v['end_date']) ? '' : date('Y-m-d H:i:s', $v['end_date']);
                $mall_imxs[$k]['img'] = empty($v['img']) ? '' : $this->G->getImgUrl($v['img']);
                $re['list'][] = $mall_imxs[$k];
            }
        }
        $this->G->success("Success", $re);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.11.17
   * ps：获取推荐商品信息
   * */
    public function getInfo()
    {
        $id = $this->request->getData('id');
        if (!is_numeric($id)) {
            $this->G->error("id_error");
            return null;
        }
        $mall_imxs = $this->MallImxs->findById($id)->first();
        $this->loadModel('MallAdblocks');
        $mall_adblocks = $this->MallAdblocks->find()
                                            ->select(['title'])
                                            ->where(['id' => $mall_imxs['mall_adblock_id']])
                                            ->first();
        $this->loadModel('Products');
        $products = $this->Products->find()
                                   ->select(['title'])
                                   ->where(['id' => $mall_imxs['product_id']])
                                   ->first();
        $mall_imxs['productTitle'] = empty($products['title']) ? '' : $products['title'];
        $mall_imxs['mall_adblock_title'] = empty($mall_adblocks['title']) ? '' : $mall_adblocks['title'];
        $mall_imxs['product_title'] = empty($products['title']) ? '' : $products['title'];
        $mall_imxs['start_date'] = empty($mall_imxs['start_date']) ? '' : date('Y-m-d H:i:s', $mall_imxs['start_date']);
        $mall_imxs['end_date'] = empty($mall_imxs['end_date']) ? '' : date('Y-m-d H:i:s', $mall_imxs['end_date']);
        $mall_imxs['img'] = empty($mall_imxs['img']) ? '' : $this->G->getImgUrl($mall_imxs['img']);
        $this->G->success("Success", $mall_imxs);
    }

}

?>
